package com.wlf.util;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.wlf.constant.Constants;
import com.wlf.constant.Fonts;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.FopFactoryConfig;
import org.apache.fop.apps.MimeConstants;
import org.plutext.jaxb.xslfo.Block;
import org.plutext.jaxb.xslfo.BorderCollapseType;
import org.plutext.jaxb.xslfo.BorderStyleType;
import org.plutext.jaxb.xslfo.ExternalGraphic;
import org.plutext.jaxb.xslfo.Leader;
import org.plutext.jaxb.xslfo.LeaderPatternType;
import org.plutext.jaxb.xslfo.Root;
import org.plutext.jaxb.xslfo.Table;
import org.plutext.jaxb.xslfo.TableBody;
import org.plutext.jaxb.xslfo.TableCell;
import org.plutext.jaxb.xslfo.TableColumn;
import org.plutext.jaxb.xslfo.TableLayoutType;
import org.plutext.jaxb.xslfo.TableRow;
import org.plutext.jaxb.xslfo.TextAlignType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.net.URL;

/**
 * @author lingfeng.wei
 * @date 2021/7/14 17:20
 */
@Component
public class PrintUtil {

    /**
     * 创建block
     * @return
     */
    public static Block createBlock(String content,String fontFamily,String fontSize,String marginTop,String marginBottom){
        Block block = new Block();
        if(StringUtils.isNotEmpty(fontFamily)) block.setFontFamily(fontFamily);

        if(StringUtils.isNotEmpty(marginBottom)) block.setMarginBottom(marginBottom);

        if(StringUtils.isNotEmpty(fontSize)) block.setFontSize(fontSize);

        if(StringUtils.isNotEmpty(marginTop)) block.setMarginTop(marginTop);

        if(StringUtils.isNotEmpty(content)) block.getContent().add(content);

        return block;
    }

    /**
     * 动态创建列的表格
     * @param tableWidth
     * @param columnWidth
     * @return
     */
    public static Table createTale(String tableWidth,String ... columnWidth){
        Table  table = new Table();
        if(tableWidth !=null && tableWidth!=null){
            if(tableWidth !=null && tableWidth !=""){
                table.setWidth(tableWidth);
            }else{
                table.setWidth("100%");
            }
            table.setBorderCollapse(BorderCollapseType.COLLAPSE);
            table.setTableLayout(TableLayoutType.FIXED);

            for(String colunm:columnWidth){
                TableColumn tableColumn = new TableColumn();
                tableColumn.getColumnWidth().add(colunm);
                table.getTableColumn().add(tableColumn);
            }
        }
        return table;
    }

    /**
     * 创建tablebody
     * @param fontSize
     * @param lineHeight
     * @param fontFamily
     * @return
     */
    public static TableBody createTablebody(String fontSize, String lineHeight, String fontFamily){
        TableBody tableBody = new TableBody();
        if(fontSize !=null && fontSize !="")
            tableBody.setFontSize(fontSize);
        if(lineHeight !=null && lineHeight !="")
            tableBody.setLineHeight(lineHeight);
        if(fontFamily !=null && fontFamily !="")
            tableBody.setFontFamily(fontFamily);
        tableBody.setKeepTogetherWithinColumn("always");
        return tableBody;
    }

    /**
     * 创建tableCell
     * @param borderWidth
     * @param textAlign
     * @param borderStyle
     * @param minHeight
     * @return
     */
    public  static TableCell createTableCell(String borderWidth,TextAlignType textAlign,BorderStyleType borderStyle,String minHeight){
        TableCell tableCell = new TableCell();

        if(StringUtils.isNotEmpty(borderWidth))
            tableCell.getBorderWidth().add(borderWidth);
        if(textAlign !=null)
            tableCell.setTextAlign(textAlign);
        if(borderStyle !=null)
            tableCell.getBorderStyle().add(borderStyle);
        if(StringUtils.isNotEmpty(minHeight))
            tableCell.setMinHeight(minHeight);

        return tableCell;
    }

    /**
     * 创建tableRow
     * @param tableRow
     * @param tableCell
     * @param content
     * @param fontFamily
     * @param fontSize
     * @return
     */
    public static TableRow createTableRow(TableRow tableRow,TableCell tableCell,String content,String fontFamily,String fontSize){
        Block block = new Block();
        if(StringUtils.isNotEmpty(fontFamily))
            block.setFontFamily(fontFamily);
        if(StringUtils.isNotEmpty(fontSize))
            block.setFontSize(fontSize);
        block.getContent().add(content);

        tableCell.getMarkerOrBlockOrBlockContainer().add(block);
        tableRow.getTableCell().add(tableCell);
        return tableRow;
    }

    /**
     * BigDecimal 转换
     * @param num
     * @return
     */
    public static BigDecimal transitionBig(BigDecimal num) {//万位计数
        if(num==null||num.equals("")) {
            num=new BigDecimal(0);
        }else {
            num=num.divide(new BigDecimal(10000));
        }

        return num;
    }

    /**
     * 划线
     * @param leader2
     * @return
     */
    public static Leader drawLine() {
        Leader leader = new Leader();
        leader.setLeaderPattern(LeaderPatternType.RULE);
        leader.getLeaderLength().add("100%");
        leader.setColor("black");
        return leader;
    }

    public static Block creaExternalGraphic(String src,String contentWidth,String contentHeight,String paddingLeft,String paddingTop){
        ExternalGraphic graphic = new ExternalGraphic();
        graphic.setSrc(src);
        graphic.setContentWidth(contentWidth);
        graphic.setContentHeight(contentHeight);
        graphic.setPaddingLeft(paddingLeft);
        graphic.setPaddingTop(paddingTop);
        Block block = new Block();
        block.getContent().add(graphic);
        return block;
    }

    /**
     * //保证模板不含有null
     * @param str
     * @return
     */
    public  static String spaceConversion(String str){
        if (str==null) {
            str="";
        }
        return str;
    }

}
